package com.block.api.controller.data;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class TestUtil {

    private static final String HOST_URL = "http://127.0.0.1:1777";
    private static final String CHANNEL_ID = "22";
    private static final String CHANNEL_SECRET = "L9GIHNAY9QBL5OC6O6R09DI9K635FY3K";

    public static void main(String[] args) throws Exception {

//        getMobileInfo();



    }




    public static String getMobileInfo(){
        String reqUrl = HOST_URL +  "/open/data/getMobileInfo";

        try {
            String orderNo = "410332";//导单的时候提供

            Map<String, Object> bizParamMap = new HashMap<>();
            bizParamMap.put("orderNo", orderNo);

            String tranData = encrypt(JSON.toJSONString(bizParamMap), CHANNEL_SECRET);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("channelId", CHANNEL_ID);
            paramMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
            paramMap.put("tranData", tranData);

            System.out.println();
            System.out.println("开始请求：");
            System.out.println(reqUrl);
            System.out.println(JSON.toJSONString(bizParamMap));
            System.out.println(JSON.toJSONString(paramMap));
            System.out.println();

            HttpRequest httpRequest = HttpUtil.createPost(reqUrl);
            String response = httpRequest.body(JSON.toJSONString(paramMap)).execute().body();

            System.out.println();
            System.out.println("请求结束：");
            System.out.println(response);
            System.out.println();

            JSONObject jsonObject = JSON.parseObject(response);
            String respCode = jsonObject.getString("respCode");
            String respMsg = jsonObject.getString("respMsg");
            JSONObject data = jsonObject.getJSONObject("data");

            if (StrUtil.equals(respCode, "200")){
                String mobile = data.getString("mobile");
                return mobile;
            }
            return "";
        }catch (Exception exception){
            exception.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String content, String aesKey) throws Exception{
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");;
        SecretKeySpec skeySpec = new SecretKeySpec(aesKey.getBytes(StandardCharsets.UTF_8), "AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(Base64.decodeBase64(content));
        return new String(encrypted);
    }

    public static String encrypt(String content, String aesKey) throws Exception{
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        SecretKeySpec skeySpec = new SecretKeySpec(aesKey.getBytes(StandardCharsets.UTF_8), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
        return Base64.encodeBase64String(encrypted);
    }


}
